package com.service.user.impl;

import com.bean.Sys_user;
import com.bean.Xtcd;
import com.common.SystemConfig;
import com.common.YhCD;
import com.dao.Sys_userMapper;
import com.dao.XtcdMapper;
import com.dao.YhjsMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import com.google.common.collect.Ordering;
import com.service.user.UserService;
import com.util.DESUtils;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by lvjianqing on 2017/9/9.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private YhjsMapper yhjsMapper;
    @Autowired
    private XtcdMapper xtcdMapper;
    @Autowired
    private Sys_userMapper sys_userMapper;

    @Override
    public void loadUserMenu(HttpServletRequest request, Sys_user user) {
        //todo
        //获取用户id
        Integer userId = user.getId();
        try {
            List<Xtcd> lst = xtcdMapper.findXtcdByUserId(userId);
            HashSet<String> cdset = new HashSet<>();
            HashMap<String, YhCD> parentMap = new HashMap<>();

            for (int i = 0; i < lst.size(); i++) {
                Xtcd xtcd = lst.get(i);
                if (xtcd.getSjbm().equals("0")) {//一级菜单
                    parentMap.put(xtcd.getBm(), new YhCD(xtcd.getMkmc(), xtcd.getUrl(), xtcd.getOrders(), xtcd.getContent()));
                }
                if(StringUtil.isNotEmpty(xtcd.getUrl())) {
                    cdset.add(xtcd.getUrl());
                }
            }
            for (int i = 0; i < lst.size(); i++){
                Xtcd xtcd = lst.get(i);
                if (!xtcd.getSjbm().equals("0")) {//二级菜单
                    if (!parentMap.containsKey(xtcd.getSjbm())) {
                        break;
                    }
                    YhCD yhcd = parentMap.get(xtcd.getSjbm());
                    yhcd.putChildCD(xtcd.getMkmc(), xtcd.getUrl(), xtcd.getOrders(), xtcd.getContent());
                    parentMap.put(xtcd.getSjbm(), yhcd);
                }
            }
            List<YhCD> yhcdList = new ArrayList<>(parentMap.values());
            Ordering<YhCD> ordering = Ordering.natural().onResultOf(new Function<YhCD, Comparable>() {
                @Override
                public Comparable apply(YhCD yhCD) {
                    return yhCD.getOrders();
                }
            });
            List<YhCD> newList = ordering.sortedCopy(yhcdList);
            request.getSession().setAttribute(SystemConfig.XtcdHasMapSessionName, newList);
            request.getSession().setAttribute(SystemConfig.XtcdHasSetSessionName, cdset);
            request.getSession().setAttribute(SystemConfig.ManagerSessionName, user);
            List<Long> jsid = yhjsMapper.findJsidByYhid(user.getId());
            request.getSession().setAttribute(SystemConfig.JsSessionName, jsid.get(0));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Sys_user getUserByLoginName(String loginName) {
        if (StringUtil.isEmpty(loginName)) {
            return null;
        }
        return sys_userMapper.selectByLoginName(loginName);
    }

    @Override
    public Page findByPage(int pageNum, int pageSize, HashMap<String, Object> paraMap) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) sys_userMapper.findAll(paraMap);
        return page;
    }

    @Override
    public Sys_user findUserById(Integer id) {
        if (id != null) return sys_userMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public int saveUser(Sys_user user) {
        Integer id = user.getId();
        if (id != null) {
            sys_userMapper.updateByPrimaryKeySelective(user);
            return 1;
        } else {
            user.setCreatetime(new Date());
            try {
                user.setPassword(DESUtils.encrypt("123456"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            sys_userMapper.insertSelective(user);
            return 0;
        }
    }
    @Override
    public void delUser(Integer id) {
        if (id != null) {
            sys_userMapper.deleteByPrimaryKey(id);
        }
    }
}
